/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.domain;

import java.io.Serializable;
import java.util.UUID;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.Immutable;

/**
 * Default implementation of {@link IArmy} annotated with persistence and injectability metadata.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 */
public class Army implements IArmy {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String overview;
    private String rulesVersion;
    @Immutable
    private final UUID uuid;
    @Transient
    private Exporter beanExporter;

    private Army(Builder builder) {
        this.name = builder.name;
        this.overview = builder.overview;
        this.rulesVersion = builder.rulesVersion;
        this.uuid = builder.uuid;
        this.beanExporter = new Exporter();
    }
    
    @Override
    public Exporter exportAsBean() {
        return beanExporter;
    }


    // Equals and hash code implementation
    // -----------------------------------------------------------------------------------------------------------------
    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((rulesVersion == null) ? 0 : rulesVersion.hashCode());
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Army)) {
            return false;
        }
        Army other = (Army) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (rulesVersion == null) {
            if (other.rulesVersion != null) {
                return false;
            }
        } else if (!rulesVersion.equals(other.rulesVersion)) {
            return false;
        }
        if (uuid == null) {
            if (other.uuid != null) {
                return false;
            }
        } else if (!uuid.equals(other.uuid)) {
            return false;
        }
        return true;
    }

    // Builder and exporter pattern
    // -----------------------------------------------------------------------------------------------------------------
    public static class Builder {
        private String name;
        private String overview;
        private String rulesVersion;
        private UUID uuid;

        public Builder() {

        }

        public Army build() {
            if (uuid == null) {
                uuid = UUID.randomUUID();
            }

            return new Army(this);
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder overview(String overview) {
            this.overview = overview;
            return this;
        }

        public Builder rulesVersion(String rulesVersion) {
            this.rulesVersion = rulesVersion;
            return this;
        }

        public Builder uuid(UUID uuid) {
            this.uuid = uuid;
            return this;
        }
    }
    
    public class Exporter implements Serializable {
        public Long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getOverview() {
            return overview;
        }

        public String getRulesVersion() {
            return rulesVersion;
        }

        public UUID getUuid() {
            return uuid;
        }
        
        private static final long serialVersionUID = 5087255090873536633L;
    }

    private static final long serialVersionUID = -4553312513319590849L;
}
